import sys
import os
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, \
	QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QComboBox,QMessageBox
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QIcon


class DrawingDialog(QDialog):
	# 触发删除图纸的信号
	signal_delete_drawing = pyqtSignal(str)
	
	def __init__(self, existing_drawings,is_eng):
		super().__init__()
		self.existing_drawings = existing_drawings
		self.is_eng=is_eng
		self.initUI()
		self.setWindowIcon(QIcon('resources/mainico.png'))
		
	def initUI(self):
		para= 'enter the drawing information' if self.is_eng else '图纸信息录入'
		self.setWindowTitle(f'{para}')
		self.setFixedSize(400, 250)
		self.setStyleSheet("""
                    QWidget {
                        background-color: #f0f0f0;
                    }
                    QLabel {
                        font-size: 20px;
                        color: #333;
                        font-weight: bold;  /* 设置标签字体加粗 */
                    }
                    QComboBox {
                        height: 30px;
                        border: 1px solid #ccc;
                        border-radius: 3px;
                        padding-left: 5px;
                        font-size: 20px;
                        font-weight: bold;  /* 设置标签字体加粗 */
                    }
                    QLineEdit {
                        height: 30px;
                        border: 1px solid #ccc;
                        border-radius: 3px;
                        padding-left: 5px;
                        background-color: white;
                        font-size: 16px;
                        font-weight: bold;
                    }
                    QPushButton {
                        height: 35px;
                        background-color: #ab7BFF;
                        color: white;
                        border: none;
                        border-radius: 3px;
                        font-size: 20px;
                        font-weight: bold;  /* 设置按钮字体加粗 */
                    }
                    QPushButton:hover {
                        background-color: #0056b3;
                    }
                """)
		
		layout = QVBoxLayout()
		
		# 已有的图纸
		existing_drawing_layout = QHBoxLayout()
		para= 'existing drawing:' if self.is_eng else '已有图纸:'
		existing_drawing_label = QLabel(f'{para}')
		self.cbx_existing_drawing = QComboBox()
		self.cbx_existing_drawing.addItems(self.existing_drawings)
		self.cbx_existing_drawing.currentTextChanged.connect(self.do_currentTextChange)
		existing_drawing_layout.addWidget(existing_drawing_label)
		existing_drawing_layout.addWidget(self.cbx_existing_drawing)
		layout.addLayout(existing_drawing_layout)
		
		# 图纸代号输入
		drawing_id_layout = QHBoxLayout()
		para= 'drawing code:' if self.is_eng else '图纸代号:'
		drawing_id_label = QLabel(f'{para}')
		self.drawing_id_input = QLineEdit()
		drawing_id_layout.addWidget(drawing_id_label)
		drawing_id_layout.addWidget(self.drawing_id_input)
		layout.addLayout(drawing_id_layout)
		
		# 客户输入
		customer_layout = QHBoxLayout()
		para= 'client:' if self.is_eng else '客    户:'
		customer_label = QLabel(f'{para}')
		self.customer_input = QLineEdit()
		customer_layout.addWidget(customer_label)
		customer_layout.addWidget(self.customer_input)
		layout.addLayout(customer_layout)
		
		# 添加图纸按钮和路径显示
		drawing_path_layout = QHBoxLayout()
		para= 'add drawing' if self.is_eng else '添加图纸'
		add_drawing_button = QPushButton(f'{para}')
		add_drawing_button.clicked.connect(self.select_drawing)
		self.drawing_path_input = QLineEdit()
		self.drawing_path_input.setReadOnly(True)
		drawing_path_layout.addWidget(add_drawing_button)
		drawing_path_layout.addWidget(self.drawing_path_input)
		layout.addLayout(drawing_path_layout)
		
		# 确认和取消按钮
		button_layout = QHBoxLayout()
		para= 'delete drawing' if self.is_eng else '删除图纸'
		self.delete_button = QPushButton(f'{para}')
		self.delete_button.clicked.connect(self.do_delete_drawing)
		para = 'OK' if self.is_eng else '确认'
		confirm_button = QPushButton(f'{para}')
		confirm_button.clicked.connect(self.accept)
		para='Cancel' if self.is_eng else '取消'
		cancel_button = QPushButton(f'{para}')
		cancel_button.clicked.connect(self.reject)
		button_layout.addWidget(confirm_button)
		button_layout.addWidget(cancel_button)
		button_layout.addWidget(self.delete_button)
		layout.addLayout(button_layout)
		
		self.setLayout(layout)
		
	
	def select_drawing(self):
		original_path = f"//TFSM/Eng/"
		initial_dir = original_path if os.path.exists(original_path) else f""
		file_path = QFileDialog.getExistingDirectory(self, '选择文件夹', initial_dir)
		if file_path:
			self.drawing_path_input.setText(file_path)
	
	def do_currentTextChange(self, string):
		self.drawing_id_input.setText(string)
	
	def do_delete_drawing(self):
		res = QMessageBox.question(self, '提醒', '是否确定删除图纸', QMessageBox.Yes | QMessageBox.No)
		if res == QMessageBox.Yes:
			self.signal_delete_drawing.emit(self.cbx_existing_drawing.currentText())


if __name__ == '__main__':
	app = QApplication(sys.argv)
	dialog = DrawingDialog([])
	if dialog.exec_() == QDialog.Accepted:
		drawing_id = dialog.drawing_id_input.text()
		customer = dialog.customer_input.text()
		drawing_path = dialog.drawing_path_input.text()
		print(f'图纸代号: {drawing_id}')
		print(f'客户: {customer}')
		print(f'图纸路径: {drawing_path}')
	sys.exit(app.exec_())
